/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wmauswertung

import javax.xml.parsers.DocumentBuilderFactory
import org.w3c.dom.Node

/**
 *
 * @author mcnanuschi
 */

class XMLParser {
    // TODO Mit diesem Groovy Script soll die XML geparsed werden und auch geschrieben werden (Speichern/Laden)
    
    def turnier = new XmlParser().parse(new File('src/wmauswertung/tournament.xml'))
    // creating empty lists
    def teams = []
    def cache = []
    // creating an empty map
    def matchScores = [:]
    
    /**
     * Function to get a list of the teams for the corresponding group
     */

    def getTeamList(String s) {   
        /* Test-prints
        //println turnier
        //println turnier.teams
        //println turnier.teams.groups
        //println turnier.teams.groups.A
        //println turnier.teams.groups.A.team1
        //println turnier.teams.groups.A.team1.name.text()
         */
        //going along the XML tree to get the right items
        turnier.groups."$s".teams.each{team ->
            team.children().each{
                teams << it.name.text()
            }
        }
        /*
        teams.each{
        println it
        }
         */
        return teams
    }

    /**
     * Function to get the results of the matches for each group
     */
    def getMatchScores(String s){
        
        turnier.groups."$s".matchScores.each{ matches ->
            matches.children().each{
                //caching the scores into a list
                cache << it.homeScore.text() << it.guestScore.text()
                // add the cached list into the map
                matchScores[it.name()] = cache
                // reinitializing list to avoid doubled entries
                cache = []
            }
        }
        return matchScores
    }

}



/* System-Property-prints
 * prints out lots of system properties
def prefix = ""
def entries = []
for (e in System.properties) {
if (e.key.startsWith(prefix)) {
entries.add(e)
}
entries.sort { e1,e2->e1.key.compareTo(e2.key) }
for (entry in entries) {
println entry
}
}
 */